<?php
namespace DataAPI\Controller;


use DataAPI\Extend\ExtendApi;
use DataAPI\Model\ExcelModel;
use DataAPI\Model\GoodSyncLogModel;
use DataAPI\Model\SupplierProductsModel;
use Org\Net\Http;
use Think\Controller;
use Think\Upload;

class IndexController extends Controller {
    public function index(){
        $this->display();
    }

    /**
     * 所有商品列表
     */
    public function goodsList(){
        $size = 20;
        $GoodsModel = new SupplierProductsModel();
        $data = $GoodsModel->getProducts(null,$size);

        $this->assign('list',$data['list']);// 赋值数据集
        $this->assign('page',$data['show']);// 赋值分页输出

        $this->display('goods_list'); // 输出模板
    }

    /**
     * 同步商品列表
     */
    public function syncList(){
        $size = 20;
        $GoodsModel = new SupplierProductsModel();
        $where = 'is_sync="0"';
        $data = $GoodsModel->getProducts(null,$size,$where);

        $this->assign('list',$data['list']);// 赋值数据集
        $this->assign('page',$data['show']);// 赋值分页输出

        $this->display('sync_list'); // 输出模板
    }

    /**
     * 同步商品
     */
    public function syncGood(){
        $ids = $_POST['ids'];
        $data = [];
        $sync = new SyncController();
        foreach($ids as $id){
            $ex = $sync->syncOneGoods($id);
            //返回成功true 失败直接返回信息；所以判断用 ===
            if($ex === true){
                $data['msg'] .= '商品'.$id.'同步成功||';
            }else{
                $data['msg'] .=$ex.  '商品'.$id.'同步失败||';
            }
        }
        $data['code'] = 200;
        $this->ajaxReturn ($data,'json');
    }
    /**
     * 日志列表
     */
    public function logsList(){
        $LogModel = new GoodSyncLogModel();
        $data = $LogModel->getLogs(20);
        $this->assign('list',$data['list']);// 赋值数据集
        $this->assign('page',$data['show']);// 赋值分页输出
        $this->display('logs_list'); // 输出模板
    }

    public function excelUpload(){
        $batches = M('sync_excel')->where(['is_sync'=>'0'])->distinct(true)->field(['batch','createdtime'])->select();
        $sync = M('sync_excel')->where(['is_sync'=>'1'])->distinct(true)->field(['batch','createdtime','synctime'])->select();
        $this->assign('arr',['batches'=>$batches,'sync'=>$sync]);
        $this->display('excel_upload');
    }
    public function syncImport(){
        $this->display('sync_import');
    }
    public function syncImportType(){
        $type = $_POST['type'];
        set_time_limit(0);
        switch($type){
            case 'Bck':
                $BcModel = new BproductsController();
                $BcModel->index();
                $data['msg'] = "导入完成";
                break;
            case 'Xilian':
                $XilModel = new WestController();
                $XilModel->index();
                $data['msg'] = "导入完成";
                break;
            default:
                $data['msg'] = "请选择需要导入的库";
                break;
        }
        $data['code'] = 200;
        $this->ajaxReturn($data,'json');
    }
    /**
     * 表格上传
     */
    public function upload(){
        session('app_id','10002');
        isset($_POST['batch'])?$_POST['batch'] = date('YmdHis',time()):$_POST['batch'];
        $batch = $_POST['batch'];
       $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    '',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('xls','xlsx','csv'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $file = new Upload($config);
        $res = $file->upload();
        if(!$res) {// 上传错误提示错误信息
            $this->error($file->getError());
        }else{
            // 上传成功
            foreach ($res as $v){
                $filePath =$file->rootPath.$v['savepath'].$v['savename'];
            }
            $excel = new ExcelModel();
            $excel->readExcel($filePath,$batch);
            $goods = json_encode(M('test_supplier_products')->where(['is_sync'=>'0','batch'=>$batch])->select());
            $this->assign('goods',$goods);
            $this->display('excel:goodslist');
            }
    }

    /**
     * 下载模板EXCEL
     */
    public function downloadMould(){
        $excleMould = './Uploads/Mould/mouldExcel.xls';
        import('Org.Net.Http');
        Http::download($excleMould);
    }

    /**
     * 批次详细
     */
    public function mybatch(){
        $batchGoods = M('test_supplier_products')->where(['batch'=>$_GET['batch']])->select();
        $this->assign('goods',json_encode($batchGoods));
        $this->display('excel:goodslist');
    }
    /**
     * 同步批次
     */
    public function syncmybatch(){
        exit;
         $batch = $_REQUEST['batch'];
         $syncApi = new SyncController();
         $syncApi->syncExcel($batch);
    }
}